<?php

namespace App\Http\Controllers\Frontend;

use Laravel\Lumen\Routing\Controller as BaseController;

use App\Model\CarModelObject;
use Illuminate\Http\Request;

class ModelObjectController extends BaseController
{

    /**
     * show all brands
     */
    public function index( Request $request, $factory, $series, $displacement ){

        $res = [
            'desc' => '查询成功！',
            'size' => '',
            'statusCode' => 1,
            'result' => []
        ];
        $factory_name = urldecode( $factory );
        $series_name = urldecode( $series );
        $displacement_name = urldecode( $displacement );
        $modelObjects = CarModelObject::modelObjectByDisplacement( $factory_name, $series_name, $displacement_name );

        foreach ( $modelObjects as $obj ) {
            $res['result'][] = [
                'c_drive_form' => $obj->c_drive_form,
                'c_engine_model' => $obj->c_engine_model,
                'c_timer_model_name' => $obj->c_timer_model_name,
                'c_transmission_type' => $obj->c_transmission_type,
                't_car_year' => $obj->t_car_year,
                'tid' => $obj->tid
            ];
        }
        $res['size'] = count( $res['result'] );
        echo json_encode( $res );

    }

    /**
     * 获取单个model object信息
     */
    public function info( Request $request, $tid ){

        $res = [
            'desc' => '查询成功！',
            'size' => 1,
            'statusCode' => 1,
            'result' => []
        ];

//        $single = CarModelObject::where('tid', $tid)->firstOrFail(); //这个语法速度非常慢
        $single = CarModelObject::infoByTid( $tid );
        if( !empty( $single ) ){
            $res['result'][] = [
                'TransmissionModel'=> $single->TransmissionModel,
                'c_drive_form'=>$single->c_drive_form,
                'c_engine_model'=>$single->c_engine_model,
                'c_timer_model_name'=>$single->c_timer_model_name,
                'c_transmission_type'=>$single->c_transmission_type,
                't_car_year'=>$single->t_car_year,
                'tid' => $tid,
            ];
        }else{
            $res = [
                'desc' => '抱歉，查询无结果！',
                'size' => 0,
                'statusCode' => -2,
                'result' => []
            ];
        }

        echo json_encode( $res );


    }

    public function oil_transmission( $tid ){
        $res = [
            'desc' => '查询成功！',
            'size' => 1,
            'statusCode' => 1,
            'result' => []
        ];
        $oil = CarModelObject::oil_transmission( $tid );
        if( !empty( $oil ) ){
            $res['result'][] = $oil;
        }else{
            $res = [
                'desc' => '抱歉，查询无结果！',
                'size' => 0,
                'statusCode' => -2,
                'result' => []
            ];
        }


        echo json_encode( $res );
    }

    public function oil_gt( $tid ){
        $res = [
            'desc' => '查询成功！',
            'size' => 1,
            'statusCode' => 1,
            'result' => []
        ];

        $oil = CarModelObject::oil_gt( $tid );
        if( !empty( $oil ) ){
            $res['result'][] = $oil;
        }else{
            $res = [
                'desc' => '抱歉，查询无结果！',
                'size' => 0,
                'statusCode' => -2,
                'result' => []
            ];
        }

        echo json_encode( $res );
    }

    public function oil_nano( $tid ){
        $res = [
            'desc' => '查询成功！',
            'size' => 1,
            'statusCode' => 1,
            'result' => []
        ];

        $oil = CarModelObject::oil_nano( $tid );
        if( !empty( $oil ) ){
            $res['result'][] = $oil;
        }else{
            $res = [
                'desc' => '抱歉，查询无结果！',
                'size' => 0,
                'statusCode' => -2,
                'result' => []
            ];
        }
        echo json_encode( $res );
    }
}
